﻿using System;
using System.Data;

using System.Collections;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class ThucDonDAO : DataProvider
    {
        public void Them(ThucDonDTO info)
        {
            connect();
            string insertCommand = "INSERT INTO ThucDon VALUES('" +
                info.NGAYAPDUNG.ToShortDateString() + "', '" +
                info.BUOIAPDUNG + "')";

            executeNonQuery(insertCommand);
            disconnect();
        }

        public void Xoa(int maTD)
        {
            connect();
            string deleteCommand = "DELETE FROM ThucDon WHERE thuc_don_id = " + maTD + "";
            executeNonQuery(deleteCommand);
            disconnect();
        }

        public ArrayList getDsThucDon()
        {
            connect();
            string sql = "select * from ThucDon";
            adapter = new SqlDataAdapter(sql, connection);
            DataSet dataset = new DataSet();
            adapter.Fill(dataset);
            ArrayList arr = ConvertDataSetToArrayList(dataset);
            disconnect();
            return arr;
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            ThucDonDTO tdDTO = new ThucDonDTO();
            tdDTO.STT = int.Parse(dt.Rows[i][0].ToString());
            tdDTO.SOLUONG = int.Parse(dt.Rows[i][1].ToString());
            tdDTO.NGAYAPDUNG = DateTime.Parse(dt.Rows[i][2].ToString());
            tdDTO.BUOIAPDUNG = dt.Rows[i][3].ToString();

            return (object)tdDTO;
        }

        public ArrayList TimKiem(DateTime thoigian)
        {
            connect();
            string sql = "select * from ThucDon where ngay_ap_dung = '" + thoigian + "'";
            adapter = new SqlDataAdapter(sql, connection);
            DataSet dataset = new DataSet();
            adapter.Fill(dataset);
            ArrayList arr = ConvertDataSetToArrayList(dataset);
            disconnect();
            return arr;
        }

        public void ThemThucDon(ThucDonDTO td)
        {
            connect();
            string insertCommand = "INSERT INTO ThucDon(so_mon_an,ngay_ap_dung,buoi_ap_dung) VALUES(" + td.SOLUONG + ",'" + td.NGAYAPDUNG.ToShortDateString() + "', N'" + td.BUOIAPDUNG + "')";
            executeNonQuery(insertCommand);
            disconnect();
        }

        public int GetThucDonID()
        {
            connect();
            string sql = "SELECT TOP 1 thuc_don_id FROM ThucDon  ORDER BY thuc_don_id DESC";

            SqlCommand cmd = new SqlCommand(sql, connection);
            SqlDataReader reader = cmd.ExecuteReader();
            int res = 0;
            while (reader.Read())
                res = reader.GetInt32(0);

            disconnect();
            return res;
        }
    }
}
